import React, {Component} from 'react';
import Table from "../Public/Table";
import {NavLink} from "react-router-dom";
import http from '../../http';

class ProductCategoryTable extends Component {
	constructor(props) {
		super(props);
		this.state = {
			columns: [
				{
					title: '品类ID',
					props: 'id'
				},
				{
					title: '品类名称',
					props: 'name'
				},
				{
					title: '操作',
					render: data => {
						return (
							<div>
								<a href={data.id} onClick={(event) => this.click(data, event)}>修改名称</a>
								{
									data.parentId === 0
										?
										<NavLink to={'/product-category/index/' + data.id}>
											查看其子品类
										</NavLink>
										:
										''
								}
							</div>
						)
					}
				}
			]
		}
	}

	click = (data, event) => {
		event.preventDefault();
		let updateName = prompt('请输入新的品类名称', data.name);
		if (updateName && updateName !== data.name) {
			http.setCategoryName({
				categoryId: data.id,
				categoryName: updateName
			}).then(res => {
				if (res.status === 0) {
					alert(res.data);
				}
			})
		}
	};

	render() {
		return (
			<div>
				<Table columns={this.state.columns} data={this.props.list} emptyInfo="正在加载中~"/>
			</div>
		);
	}
}

export default ProductCategoryTable;